var app = getApp();
var utils = require('../../utils/util.js');
Page({
  data: {
    //头像
    pictrue: 'https://cmhdtstatic.oss-cn-beijing.aliyuncs.com/add_pic.png',
    //性别
    sex: 1,
    //名称
    name: '',
    //品种
    breed: '',
    //年
    yearRange: [],
    yearValue: 0,
    //月
    monthRange: [],
    monthValue: 0,
    //日
    dayRange: [],
    dayValue: 0,
    //是否点击过
    isSubmit: false
  },
  onLoad: function(options) {
    var sequence = options.sequence;
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var monthRange = [];
    for (var i = 1; i <= 12; i++) {
      monthRange.push(i);
    }
    var day = date.getDate();
    var yearRange = [];
    for (var i = 0; i < 15; i++) {
      yearRange.push(year - i);
    }
    this.setData({
      yearRange: yearRange
    })
    var dayRange = [];
    switch (month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        for (var i = 1; i <= 31; i++) {
          dayRange.push(i);
        }
        break;
      case 2:
      case 4:
      case 6:
      case 9:
      case 11:
        for (var i = 1; i <= 30; i++) {
          dayRange.push(i);
        }
        break;
    }



    this.setData({
      dayRange: dayRange,
      monthValue: month - 1,
      dayValue: day - 1,
      sequence: sequence,
      monthRange: monthRange
    })
  },

  //选择男
  selMan: function() {
    var sex = this.data.sex;
    if (sex == 1) {
      return;
    } else {
      this.setData({
        sex: 1
      })
    }
  },
  //选择女
  selWoMan: function() {
    var sex = this.data.sex;
    if (sex == 0) {
      return;
    } else {
      this.setData({
        sex: 0
      })
    }
  },
  //选择年
  seleYear: function(e) {
    var index = e.detail.value;
    this.setData({
      yearValue: index,
    })

  },
  //选择年
  seleMonth: function(e) {
    var index = e.detail.value;
    this.setData({
      monthValue: index,

    })
  },
  //选择年
  seleDay: function(e) {
    var index = e.detail.value;
    this.setData({
      dayValue: index,

    })
  },
  //添加图片
  addPic: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function(r) {
        that.setData({
          pictrue: r.tempFiles[0].path
        })
      },
    })
  },
  //输入名称
  inputName: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过5
    if (cursor > 5) {
      this.setData({
        name: value.substring(0, 5)
      })
    } else {
      this.setData({
        name: value
      })
    }
  },

  //输入品种
  inputBreed: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过5
    if (cursor > 10) {
      this.setData({
        breed: value.substring(0, 10)
      })
    } else {
      this.setData({
        breed: value
      })
    }
  },
  //返回
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  //添加
  addPet: function() {
    var pictrue = this.data.pictrue;
    var name = this.data.name;
    var breed = this.data.breed;
    if (pictrue == 'https://cmhdtstatic.oss-cn-beijing.aliyuncs.com/add_pic.png') {
      wx.showToast({
        title: '请上传宠物头像',
        icon: 'none',
        duration: 3000
      })
      return;
    }



    if (name == '') {
      wx.showToast({
        title: '请输入宠物名字',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (breed == '') {
      wx.showToast({
        title: '请输入宠物品类',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    var yearValue = this.data.yearRange[this.data.yearValue];
    var monthValue = this.data.monthRange[this.data.monthValue];
    if (monthValue < 10) {
      monthValue = '0' + monthValue;
    }
    var dayValue = this.data.dayRange[this.data.dayValue];
    if (dayValue < 10) {
      dayValue = '0' + dayValue;
    }
    var birthday = yearValue + '-' + monthValue + '-' + dayValue + ' ' + '00:00:00.000';

    var date = new Date();
    var year1 = date.getFullYear();
    var month1 = date.getMonth()+1;
    var day1 = date.getDate();
    if (yearValue == year1) {
      if (monthValue > month1) {
        wx.showToast({
          title: '请选择不大于今天的日期',
          icon: 'none',
          duration: 3000
        })
        return;
      }
      if (dayValue > day1) {
        wx.showToast({
          title: '请选择不大于今天的日期',
          icon: 'none',
          duration: 3000
        })
        return;
      }
    }
    var gender = this.data.sex;

    var sequence = this.data.sequence;
    var that = this;

    //如果点击过
    if (this.data.isSubmit) {
      return;
    }
    this.data.isSubmit = true;
    wx.request({
      url: app.globalData.requestUrl + 'getUploadFileOssPolicy',
      method: 'GET',
      data: {
        userId: app.globalData.user.id,
        type: 1
      },
      success: function(r) {
        if (r.data.code == 200) {
          //生成名称
          var uuid = utils.getUUID();
          //获取类型
          var mimeType = utils.getMimeType(pictrue);
          var src = uuid + mimeType
          var host = r.data.data.host
          wx.uploadFile({
            url: host,
            filePath: pictrue,
            name: 'file',
            formData: {
              key: src,
              policy: r.data.data.policy,
              OSSAccessKeyId: r.data.data.accessid,
              success_action_status: '200',
              signature: r.data.data.signature
            },
            success: function(r) {
              wx.request({
                url: app.globalData.requestUrl + 'addPet',
                method: 'POST',
                data: {
                  belongUserId: app.globalData.user.id,
                  name: name,
                  gender: gender,
                  breed: breed,
                  birthday: birthday,
                  pictrue: host + src,
                  sequence: sequence,

                },
                success: function(r) {
                  if (r.data.code == 200) {
                    wx.showToast({
                      title: '保存爱宠成功',
                      duration: 2000
                    })
                    var timeOut = setTimeout(function() {
                      clearTimeout(timeOut);
                      wx.navigateBack({
                        delta: 1
                      })
                    }, 3000)

                    that.getAllPetByUserId();
                  } else {
                    wx.showToast({
                      title: '保存爱宠失败,请稍后重试',
                      icon: 'none',
                      duration: 3000
                    })
                  }
                },
                fail: function() {
                  wx.showToast({
                    title: '保存爱宠失败,请稍后重试',
                    icon: 'none',
                    duration: 3000
                  })
                },
              })
            },
            fail: function() {
              wx.showToast({
                title: '保存爱宠失败,请稍后重试',
                icon: 'none',
                duration: 3000
              })
            },
          })
        } else {
          wx.showToast({
            title: '保存爱宠失败,请稍后重试',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,保存爱宠失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //获取宠物列表
  getAllPetByUserId: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getAllPetByUserId',
      data: {
        userId: app.globalData.user.id
      },
      success: function(r) {
        if (r.data.code == 200) {
          var petList = r.data.data;
          app.globalData.petList = petList;
        } else {
          wx.showToast({
            title: '获取宠物资料失败,请稍后重试',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取宠物资料失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
})